package com.xtaller.lazy.lib.convert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.var;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-11
 * @Description: list处理类
 */
public class L {

    public static JSONArray list2Tree(Object list, Long parentId) {
        var objects = J.o2l(list);
        JSONArray children = new JSONArray();
        Iterator var3 = objects.iterator();

        while(var3.hasNext()) {
            JSONObject object = (JSONObject)var3.next();
            Long id = object.getLong("id");
            Long pid = object.getLong("parentId");
            if (parentId.equals(pid)) {
                JSONArray node = list2Tree(objects, id);
                object.put("children", node);
                children.add(object);
            }
        }

        return children;
    }

    public static JSONArray list2Tree(List<JSONObject> objects, String parentId, int weight) {
        JSONArray children = new JSONArray();
        Iterator var4 = objects.iterator();

        while(var4.hasNext()) {
            JSONObject object = (JSONObject)var4.next();
            String id = object.getString("id");
            String pid = object.getString("parentId");
            if (parentId.equals(pid)) {
                object.put("weight", weight);
                object.put("text", object.getString("name"));
                JSONArray node = list2Tree(objects, id, weight + 1);
                object.put("children", node);
                children.add(object);
            }
        }

        return children;
    }

}
